<!-- eslint-disable no-console -->
<!-- 质量管理模块 - 待检任务 -->
<script lang="ts" setup>
import type { VbenFormProps } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { DictApi, pendinginspectApi } from '#/api';

import { reactive } from 'vue';

import { useVbenModal } from '@vben/common-ui';
import { VxeButton } from '@vben/plugins/vxe-table';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { DictResp, getPendinginspectList } from '#/api';
// import {} from 'element-plus';
// 将字典数据添加到数组中;
const mes_qc_type = reactive<DictApi.SelectOption[]>([]);
const mes_qc_detail_type = reactive<DictApi.SelectOption[]>([]);
DictResp(['mes_qc_type', 'mes_qc_detail_type']).then((res) => {
  // 将两个数组合并
  if (res.mes_qc_type) {
    mes_qc_type.push(...res.mes_qc_type);
    // const mesindex = mes_index_type.map((item) => ({ value: item.value }));
    // console.log(mesindex);
  }
  if (res.mes_qc_detail_type) {
    mes_qc_detail_type.push(...res.mes_qc_detail_type);
  }
});

const [EditModal, editmodalApi] = useVbenModal({
  // 连接抽离的组件
  showConfirmButton: false,
  showCancelButton: false,
});

const formOptions: VbenFormProps = {
  // 默认展开
  collapsed: false,
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入单据编码',
      },
      fieldName: 'sourceDocCode',
      label: '单据编码',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入物资名称',
      },
      fieldName: 'itemName',
      label: '物资名称',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: mes_qc_type,
        placeholder: '请选择检测类型',
      },
      fieldName: 'qcType',
      label: '检测类型',
    },
  ],
  // 控制表单是否显示折叠按钮
  showCollapseButton: false,
  submitButtonOptions: {
    content: '搜索',
  },
  // 是否在字段值改变时提交表单
  submitOnChange: false,
  // 按下回车时是否提交表单
  submitOnEnter: false,
};

const gridOptions: VxeGridProps<pendinginspectApi.Row> = {
  checkboxConfig: {
    highlight: true,
    labelField: 'name',
  },
  columns: [
    {
      align: 'left',
      field: 'itemId',
      title: '',
      type: 'checkbox',
      width: 50,
    },
    { field: 'sourceDocCode', title: '来源单据编号' },
    {
      field: 'qcType',
      title: '检测类型',
      formatter: ({ cellValue }) => {
        const item = mes_qc_type.find((item) => item.value === cellValue);
        return item?.label || '';
      },
    },
    {
      field: 'qcDetailType',
      title: '详细检测类型',
      formatter: ({ cellValue }) => {
        const item = mes_qc_detail_type.find(
          (item) => item.value === cellValue,
        );
        return item?.label || '';
      },
    },
    { field: 'itemCode', title: '物资编码' },
    { field: 'itemName', title: '物资名称' },
    { field: 'quantityCheck', title: '待检数量' },
    { field: 'unitName', title: '单位' },
    { slots: { default: 'action' }, title: '操作' },
  ],
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        const res = await getPendinginspectList({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
        return {
          total: res.total,
          items: res.rows,
        };
      },
    },
  },

  toolbarConfig: {
    // 是否显示搜索表单控制按钮
    // @ts-ignore 正式环境时有完整的类型声明
    search: true,
    refresh: { code: 'query' },
  },
};

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

function editRowEvent(row: pendinginspectApi.Row) {
  console.log(row);
  editmodalApi
    .setState({
      title: '修改质检方案',
    })
    .setData({ row })
    .open();
  gridApi.grid?.setEditRow(row);
}
</script>

<template>
  <div>
    <EditModal />
    <Grid>
      <template #link="{ row }">
        <VxeButton mode="text" @click="editRowEvent(row)" status="primary">
          {{ row.templateCode }}
        </VxeButton>
      </template>
      <template #action="{ row }">
        <VxeButton
          mode="text"
          icon="vxe-icon-signature"
          @click="editRowEvent(row)"
          status="primary"
        >
          {{ row.qcType }}
        </VxeButton>
      </template>
    </Grid>
  </div>
</template>
